var layer = layui.layer //弹层
    ,table = layui.table //表格
    ,form = layui.form,
    $ = layui.jquery,
    laydate = layui.laydate;

layui.use(['tableEdit',"excel"], function () {
    let tableEdit = layui.tableEdit;
    let layer = layui.layer,
        excel = layui.excel;


    lay('.starttime').each(function(){
        laydate.render({
            elem: this
        });
    });

    function getAllJson(jsons) {
        let tmpitem = []
        for (let item of jsons) {
            let tmparr = {}
            tmparr["value"] = item["code"];
            tmparr["name"] = item["name"];
            if (item["users"]) {
                for (let it of item["users"]) {
                    item["list"].push(it)
                }
            }
            if (item["list"] && item["list"].length) {
                tmparr["children"] = getAllJson(item["list"]);
                tmpitem.push(tmparr)
            } else {
                tmpitem.push(tmparr)
            }

        }
        return tmpitem
    }

    function getxmselect(el, name, data) {
        return xmSelect.render({
            el: el,
            name: name,
            tree: {
                show: true,
                showLine: true,
                showFolderIcon: true,
                indent: 20,
                expandedKeys: false
            },
            height: 'auto',
            data: data,
            on: function ({arr, change, isAdd}) {
                return this
            }
        })
    }
    let salesmanList=[];
    layer.ready(function () {
        //获取当前用户的部门及人员结构树
        let treeList = getCurrentDepartmentUser();

        if (treeList && treeList.list) {
            let userTree = getAllJson(treeList.list);
            salesmanList=getxmselect("#salesmanCode", "salesmanCode", userTree);
        } else {
            salesmanList=getxmselect("#salesmanCode", "salesmanCode", [{name: user.name, value: user.code}])
        }

    });

    //表单刷新
    form.render();
    let initData = [];
    table.render({
        elem: '#paymentSelect'
        ,data:initData
        ,toolbar: '#paymentToolbar'
        ,totalRow: true
        ,cols: [[
              {field: 'numbers',width: 50,type:'numbers',title:'序号'}
            , {field: 'billNo', title: '外销合同号', sort: true, width: 200, align: 'center'}
            , {field: 'signingDate', title: '签约日期', sort: true, width: 200, align: 'center'}
            , {field: 'salesmanName', title: '业务员', sort: true, width: 200, align: 'center'}
            , {field: 'productCode', title: '产品编码', sort: true, width: 200, align: 'center'}
            , {field: 'specificationCode', title: '规格编码', sort: true, width: 200, align: 'center'}
            , {field: 'productNum', title: '销售数量', sort: true, width: 200, align: 'center'}
            , {field: 'productCName', title: '中文名称', sort: true, width: 120, align: 'center'}
            , {field: 'productEName', title: '英文名称',sort:true,width: 120, align: 'center'}
            , {field: 'productCDesc', title: '中文描述', sort: true, width: 120, align: 'center'}
            , {field: 'productEDesc', title: '英文描述', sort: true, width: 120, align: 'center'}
            , {field: 'transferNum', title: '已调拨数量', sort: true, width: 200, align: 'center'}
            , {field: 'unTransferNum', title: '未调拨数量',sort:true,width: 120, align: 'center'}
        ]]
        ,id: 'maininformationtableid'
        ,height: $(document).height() - $('#paymentSelect').offset().top - 75,
        //开启分页
        page: true,
        limits: [300,600,900],
    });


    //搜索提交
    form.on('submit(submitform)', function(data){
        table.reload('maininformationtableid', {
            page: {
                limit:300,
                curr: 1
            },
            url:'transfer/statisticsTransferAndContractByPage',
            where:  data.field
        }, 'data');
        return false;
    });
    document.getElementById('clear').onclick = function(){
        salesmanList.setValue([ ]);
    };
});
